<template>
  <el-container class="mainContent">
    <el-row>
      <el-col class="title">修改签约居民信息</el-col>
    </el-row>
    <el-form ref="form" :model="form" label-width="150px">
      <el-form-item label="居民姓名：">
        <el-input v-model="form.c_name"></el-input>
      </el-form-item>
      <el-form-item label="性别">
        <el-select v-model="form.c_sex" placeholder="请选性别">
          <el-option label="男" value="男"></el-option>
          <el-option label="女" value="女"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="居民照片：">
        <el-image style="width: 200px; height: 200px" :src="url" fit="cover"></el-image>
      </el-form-item>
      <el-form-item label="身份证号：">
        <el-input v-model="form.c_sid"></el-input>
      </el-form-item>
      <el-form-item label="手机号：">
        <el-input v-model="form.c_tel"></el-input>
      </el-form-item>
      <el-form-item label="居住地址：">
        <el-input v-model="form.c_address"></el-input>
      </el-form-item>
      <el-form-item label="人群分类：">
        <el-checkbox-group v-model="type">
          <el-checkbox label="0~6岁儿童" name="type"></el-checkbox>
          <el-checkbox label="老年人" name="type"></el-checkbox>
          <el-checkbox label="孕产妇" name="type"></el-checkbox>
          <el-checkbox label="高血压" name="type"></el-checkbox>
          <el-checkbox label="糖尿病" name="type"></el-checkbox>
          <el-checkbox label="肺结核" name="type"></el-checkbox>
          <el-checkbox label="严重精神障碍" name="type"></el-checkbox>
          <el-checkbox label="残疾人" name="type"></el-checkbox>
          <el-checkbox label="健康居民" name="type"></el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-row>
        <el-col class="subtitle">签约服务包管理：</el-col>
      </el-row>
      <el-form-item label="服务包选择：">
        <el-select v-model="serverTypes" placeholder="请选择">
          <el-option label="高血压" value="高血压"></el-option>
          <el-option label="糖尿病" value="糖尿病"></el-option>
          <el-option label="孕产妇" value="孕产妇"></el-option>
          <el-option label="0~6岁儿童" value="0~6岁儿童"></el-option>
          <el-option label="公共卫生基础服务" value="公共卫生基础服务"></el-option>
        </el-select>
        <el-select v-model="serverPackTypes" placeholder="请选择">
          <el-option label="初级包" value="初级包"></el-option>
          <el-option label="中级包" value="中级包"></el-option>
          <el-option label="高级包" value="高级包"></el-option>
        </el-select>
        <el-button @click.prevent="addMember()">添加服务</el-button>
      </el-form-item>
      <el-form-item>
        <el-table :data="tableData" style="width: 100%">
          <el-table-column prop="serverTypes" label="预选服务包种类" width="400"></el-table-column>
          <el-table-column prop="serverPackTypes" label="已选服务包类型" width="300"></el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">保存</el-button>
        <el-button @click="back">返回</el-button>
      </el-form-item>
    </el-form>
  </el-container>
</template>
<script>
export default {
  name: "EditContractManagement",
  data() {
    return {
      form: {},
      list: [],
      c_id: 0,
      serverTypes: "",
      serverPackTypes: "",
      tableData: [],
      baseUrl: "http://127.0.0.1:8001/",
      url: "",
      type: [],
    };
  },
  methods: {
    getData() {
      this.axios({
        url: "/getContractData",
        method: "post",
        data: { c_id: Number(this.c_id) },
      })
        .then((res) => {
          console.log(res);
          if (res.data.code === 200) {
            this.form = res.data.data;
            this.type = res.data.data.c_type;
            this.tableData = res.data.data.serverPack;
            this.url = this.baseUrl + res.data.data.c_imgUrl;
            console.log(res.data.data.c_type);
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    addMember() {
      let obj = {};
      obj.id = this.type.length + 1;
      obj.serverPackTypes = this.serverPackTypes;
      obj.serverTypes = this.serverTypes;
      this.tableData.push(obj);
    },
    handleDelete(index, row) {
      this.$confirm("是否删除该签约包, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        var index = this.tableData.indexOf(row);
        this.tableData.splice(index, 1);
      })
      .catch(()=>{
           this.$message({
            type: "info",
            message: "已取消删除",
          });
      })
    },
    onSubmit() {
      this.form.c_type = this.type;
      console.log(this.form);
      this.$confirm("是否修改此居民信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.axios({
            url: "/editContract",
            method: "post",
            data: this.form,
          })
            .then((res) => {
              if (res.data.code === 200) {
                this.$router.push("/home/contract");
                this.$message({
                  message: "修改成功",
                  type: "success",
                });
              } else {
                this.$message.error("修改失败");
              }
            })
            .catch((err) => {
              this.$message.error("修改失败");
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    back() {
      this.$router.go(-1);
    },
  },
  mounted() {
    this.c_id = sessionStorage.getItem("c_id");
    this.getData();
  },
};
</script>
<style lang="less" scoped>
.form-inline {
  width: 100%;
  height: 300px;
  text-align: left;
}
.mainContent {
  width: 100%;
  flex-direction: column;
  text-align: left;
}
.search-layer {
  width: 100%;
}
.pagination {
  margin-top: 50px;
  text-align: center;
}
.title {
  margin-bottom: 30px;
  font-size: 20px;
  font-weight: 600;
}
.subtitle {
  margin: 30px 0;
  font-size: 16px;
  font-weight: 600;
}
</style>